﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ControlAccessUser.Presenter;
using ControlAccessUser.DataAccess;
using ControlAccessUser.Model;

namespace ControlAccessUser.Tests
{
    [TestClass]
    public class ReportsOfLoginsTest
    {
        MockReportsOfLoginsView vReportsOfLoginView;
        PresenterReportsOfLogins pReportsOfLogin;

        [TestInitialize]
        public void Initial()
        {
            vReportsOfLoginView = new MockReportsOfLoginsView();
            pReportsOfLogin = new PresenterReportsOfLogins(vReportsOfLoginView);

            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.Users.Add(new User() { UserName = "gary", UserPassword = "123456" });
            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.SaveChanges();
            Login Login1 = new Login() 
            {
                UserId=SearchUserByUserName("gary").UserId,
                DataTimeLoginUser=(DateTime.Now).AddDays(-5),
                DataTimeLogoutUser=(DateTime.Now)
            };
            Login Login2 = new Login()
            {
                UserId = SearchUserByUserName("gary").UserId,
                DataTimeLoginUser = (DateTime.Now).AddDays(4),
                DataTimeLogoutUser = (DateTime.Now).AddDays(10)
            };

            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.Logins.Add(Login1);
            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.Logins.Add(Login2);
            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.SaveChanges();
        }

        [TestCleanup]
        public void Clean()
        {
            ControlAccessUser.DataAccess.ControlAccessUserEntities.Instance.LimpiarDB();
        }

        private Model.User SearchUserByUserName(string UserName)
        {
            return (from user in ControlAccessUserEntities.Instance.Users
                    where user.UserName == UserName
                    select user
                    ).ToList().First();
        }
        [TestMethod]
        public void ViewReportOfLoginBySession()
        {
            Model.User User = SearchUserByUserName("gary");
            vReportsOfLoginView.SetReportOfLoginBySession(User);
            pReportsOfLogin.SearchLoginsByUser();

            Assert.AreEqual(2, vReportsOfLoginView.Logins.Count);
        }

        [TestMethod]
        public void ViewReportOfLoginsByWeek()
        {
            Model.User User = SearchUserByUserName("gary");
            vReportsOfLoginView.SetReportOfLoginByWeek(User);
            pReportsOfLogin.SearchLoginsByUser();

            Assert.AreEqual(2, vReportsOfLoginView.Logins.Count);
        }

        [TestMethod]
        public void ViewReportOfLoginByMonth()
        {
            Model.User User = SearchUserByUserName("gary");
            vReportsOfLoginView.SetReportOfLoginByMonth(User);
            pReportsOfLogin.SearchLoginsByUser();
            Assert.AreEqual(1, vReportsOfLoginView.Logins.Count);
        }
    }
}
